package com.verisette.ncos.common;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class CommonUtil {

	  // convert date to String format
	public static String date2StrFormat(Date date, String format){
		DateFormat dateFormat2 = new SimpleDateFormat(format,Locale.ENGLISH);
	    String strDate2 = dateFormat2.format(date);
	        
	    return strDate2;
	}
	
	  // convert dd/MM/yyyy HH:mm:sss to Date
	public static Date convertDateTimeStrFormatToDate(String dateStr){
		DateFormat inputFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:sss",Locale.ENGLISH);
			
		if(dateStr == null || "".equals(dateStr)){
			return null;
		}
			
		try {
			return inputFormat.parse(dateStr);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}		
	}
	
	public static Date toDate(String dateStr){
		DateFormat inputFormat = new SimpleDateFormat("dd/MM/yyyy", Locale.ENGLISH);
		
		if(dateStr == null || "".equals(dateStr)){
			return null;
		}
		
		try {
			return inputFormat.parse(dateStr);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}	
	}
	
	public static String dbDateFormat(Date date){
		DateFormat dateFormat2 = new SimpleDateFormat("yyyyMMdd HHmmss" ,Locale.ENGLISH);
		String strDate2 = dateFormat2.format(date);
		return strDate2;
	}
	
	public static String uiDateFormat(String date){
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss",new Locale("th","TH"));
		DateFormat dateFormat2 = new SimpleDateFormat("yyyyMMdd HHmmss" ,Locale.ENGLISH);
		DateFormat dateFormat3 = new SimpleDateFormat("yyyyMMdd" ,Locale.ENGLISH);
		DateFormat dateFormat4 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S" ,Locale.ENGLISH);
		Date date2;
		try {
			date2 = dateFormat4.parse(date);
		} catch (ParseException e1) {
			try {
				date2 = dateFormat2.parse(date);
			} catch (ParseException e2) {
				try {
					date2 = dateFormat3.parse(date);
				} catch (ParseException e3) {
					return date;
				}
			}
		}
		return dateFormat.format(date2);
	}
	
	public static String uiDateFormat2(String date){
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy",new Locale("th","TH"));
		DateFormat dateFormat2 = new SimpleDateFormat("yyyyMMdd HHmmss" ,Locale.ENGLISH);
		DateFormat dateFormat3 = new SimpleDateFormat("yyyyMMdd" ,Locale.ENGLISH);
		Date date2;
		try {
			date2 = dateFormat2.parse(date);
		} catch (ParseException e) {
			try {
				date2 = dateFormat3.parse(date);
			} catch (ParseException e1) {
				return date;
			}
		}
		return dateFormat.format(date2);
	}
	
	public static String uiDobDoeDateFormat(String date){
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy",new Locale("th","TH"));
		DateFormat dateFormat3 = new SimpleDateFormat("yyyyMMdd" ,Locale.ENGLISH);
		
		Date date2;
		try {
			date2 = dateFormat3.parse(date);
		} catch (ParseException e1) {
			return date;
		}
		if (!date.equals(dateFormat3.format(date2))) {
			return date;
		}
		return dateFormat.format(date2);
	}
	
	public static String dbDateFormat2(String date){
		DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy",new Locale("th","TH"));
		DateFormat dateFormat2 = new SimpleDateFormat("yyyyMMdd" ,Locale.ENGLISH);
		Date date2;
		try {
			date2 = dateFormat.parse(date);
		} catch (ParseException e) {
			return date;
		}
		return dateFormat2.format(date2);
	}

	public static String uiDobDoeDateFormatNoSlash(String date){
		DateFormat dateFormat = new SimpleDateFormat("ddMMyyyy",new Locale("th","TH"));
		DateFormat dateFormat3 = new SimpleDateFormat("yyyyMMdd" ,Locale.ENGLISH);
		
		Date date2;
		try {
			date2 = dateFormat3.parse(date);
		} catch (ParseException e1) {
			return date;
		}
		if (!date.equals(dateFormat3.format(date2))) {
			return date;
		}
		return dateFormat.format(date2);
	}
}
